#include <fstream>
#include "protos/addressbook.pb.h"

void ListPeople(const tutorial::AddressBook& book)
{
    for (int i = 0; i < book.people_size(); ++i)
    {
        const auto& p = book.people(i);
        std::cout << "ID: " << p.id() << "\n"
                  << "Name: " << p.name() << "\n";
        if (p.has_email())
        {
            std::cout << "Email: " << p.email() << "\n";
        }
        for (int j = 0; j < p.phones_size(); ++j)
        {
            const auto& ph = p.phones(j);
            // 根据 ph.type() 输出类型
            std::cout << "Phone: " << ph.number() << "\n";
        }
    }
}

int main(int argc, char* argv[])
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;
    tutorial::AddressBook address_book;
    std::fstream          input(argv[1], std::ios::in | std::ios::binary);
    if (!address_book.ParseFromIstream(&input))
    {
        std::cerr << "解析失败\n";
        return -1;
    }
    ListPeople(address_book);
    google::protobuf::ShutdownProtobufLibrary();
    return 0;
}
